package com.zmhz.intercept;

import com.eova.aop.AopContext;
import com.eova.aop.MetaObjectIntercept;
import com.eova.common.Easy;
import com.eova.common.utils.xx;
import com.eova.common.utils.time.DateUtil;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class UsersInfoInterceptor extends MetaObjectIntercept {

	@Override
	public void queryAfter(AopContext ac) throws Exception {
		Record record = ac.record;
		if (record != null) {
			String sql = "select province as provincecode,city as citycode,region as regioncode,gender from users_info  where userid=?";
			Record record2 = Db.findFirst(sql, record.getInt("userid"));
			record.setColumns(record2);
			record.remove("userid");
		}

	}

	/*
	 * @Override public void addInit(AopContext ac) throws Exception { Integer
	 * userid = ac.fixed.getInt("userid"); String sql =
	 * "select count(*) from users_info wehre userid=?"; Integer count =
	 * Db.queryInt(sql, userid);
	 * 
	 * }
	 */
	/**
	 * 添加用户会员信息后修改用户信息的状态为会员用户
	 */
	@Override
	public String addAfter(AopContext ac) throws Exception {
		Integer userid = ac.record.getInt("userid");

		String sql = "select usertype,regtime from frontusers  where id=?";
		Record record = Db.findFirst(sql, userid);
		Integer usertype = record.getInt("usertype");
		long days = DateUtil.getIntevalDays(DateUtil.getCurrDate(), record.getDate("regtime"));
		// 当免费会员超过180天，会员状态设为“2=会员过期”;===后期此处应该加上收费会员的判断===
		if (days > 180) {
			sql = "update frontusers set userstatus=2 where id=?";
			Db.update(sql, userid);
		} else {
			String name = ac.record.getStr("name");
			String province = ac.record.getStr("province");
			String city = ac.record.getStr("city");
			String region = ac.record.getStr("region");
			String address = ac.record.getStr("address");
			// 当必要的会员信息填写好后，可以更新用户表的用户状态
			if (null != name && null != province && null != city && null != region && null != address) {
				if (usertype == 0) {// 个人用户
					sql = "update frontusers set userstatus=1 where id=?";
					Db.update(sql, userid);
				} else {// 企业用户
					sql = "select province ,city ,region ,address  from corporate_info  where userid=?";
					record = Db.findFirst(sql, userid);
					if (!xx.isEmpty(record)) {
						province = record.getStr("province");
						city = record.getStr("city");
						region = record.getStr("region");
						address = record.getStr("address");
						if (null != province && null != city && null != region && null != address) {
							sql = "update frontusers set userstatus=1 where id=?";
							Db.update(sql, userid);
						}
					}

				}
			}
		}

		return null;
	}

	/**
	 * 删除会员信息时修改用户状态为注册用户
	 */
	@Override
	public String deleteAfter(AopContext ac) throws Exception {
		Integer userid = ac.record.getInt("userid");
		String sql = "update frontusers set usertype=0 where id=?";
		Db.update(sql, userid);

		return null;
	}

	@Override
	public String addSucceed(AopContext ac) throws Exception {
		Integer userid = ac.records.get(0).getInt("userid");
		String sql = "select token,userstatus,usertype from frontusers  where id=?";
		Record record = Db.findFirst(sql, userid);
		Easy easy = new Easy();
		if (!xx.isEmpty(record)) {
			easy.setData(record);
			easy.setSuccess(true);
			easy.setMsg("操作成功");
		} else {
			easy.setData(null);
			easy.setSuccess(false);
			easy.setMsg("操作失败");
		}

		return JsonKit.toJson(easy);
	}

	@Override
	public String updateBefore(AopContext ac) throws Exception {
		Integer userid = ac.record.getInt("userid");

		String sql = "select count(*) from users_info as a1 " + " left join frontusers as a2 on a1.userid=a2.id "
				+ " where a2.id=?";

		Integer count = Db.queryInt(sql, userid);
		// 修改会员信息时企业信息不存在先添加会员信息
		if (count == 0) {
			Db.save("users_info", ac.record);
		}
		return null;
	}

	@Override
	public String updateAfter(AopContext ac) throws Exception {

		Integer userid = ac.record.getInt("userid");

		String sql = "select usertype,regtime from frontusers  where id=?";
		Record record = Db.findFirst(sql, userid);
		Integer usertype = record.getInt("usertype");
		long days = DateUtil.getIntevalDays(DateUtil.getCurrDate(), record.getDate("regtime"));
		// 当免费会员超过180天，会员状态设为“2=会员过期”;===后期此处应该加上收费会员的判断===
		if (days > 180) {
			sql = "update frontusers set userstatus=2 where id=?";
			Db.update(sql, userid);
		} else {
			sql = "select name,province,city,region,address from users_info  where userid=?";
			record = Db.findFirst(sql, userid);
			String name = record.getStr("name");
			String province = record.getStr("province");
			String city = record.getStr("city");
			String region = record.getStr("region");
			String address = record.getStr("address");
			// 当必要的会员信息填写好后，可以更新用户表的用户状态
			if (null != name && null != province && null != city && null != region && null != address) {
				if (usertype == 0) {// 个人用户
					sql = "update frontusers set userstatus=1 where id=?";
					Db.update(sql, userid);
				} else {// 企业用户
					sql = "select province ,city ,region ,address  from corporate_info  where userid=?";
					record = Db.findFirst(sql, userid);
					if (!xx.isEmpty(record)) {
						province = record.getStr("province");
						city = record.getStr("city");
						region = record.getStr("region");
						address = record.getStr("address");
						if (null != province && null != city && null != region && null != address) {
							sql = "update frontusers set userstatus=1 where id=?";
							Db.update(sql, userid);
						}
					}

				}
			}
		}

		return null;
	}

	@Override
	public String updateSucceed(AopContext ac) throws Exception {
		Integer userid = ac.records.get(0).getInt("userid");
		String sql = "select token,userstatus,usertype from frontusers  where id=?";
		Record record = Db.findFirst(sql, userid);
		Easy easy = new Easy();
		if (!xx.isEmpty(record)) {
			easy.setData(record);
			easy.setSuccess(true);
			easy.setMsg("操作成功");
		} else {
			easy.setData(null);
			easy.setSuccess(false);
			easy.setMsg("操作失败");
		}

		return JsonKit.toJson(easy);
	}
}
